#include "TerrainManager.h"

TerrainManager::TerrainManager()
{
}


TerrainManager::~TerrainManager()
{
}

void TerrainManager::Initialize(sf::RenderWindow* win){
	m_renderWindow = win;
	m_clock = new sf::Clock();
	terrainCount = 0;
	m_timer = 0.f;
	m_spawnTimer = 0.f;
	m_spawnTime = 3.5f;

	m_brlTexture = new sf::Texture();
	m_brlTexture->loadFromFile("Assets/Images/Obstacles/BARREL.png");

	m_stoneTexture = new sf::Texture();
	m_stoneTexture->loadFromFile("Assets/Images/Obstacles/RadioactiveBox.png");

	m_mineTexture = new sf::Texture();
	m_mineTexture->loadFromFile("Assets/Images/Obstacles/SeaMine.png");

	m_bloodTexture = new sf::Texture();
	m_bloodTexture->loadFromFile("Assets/Images/Damage_ Feedback2.png");


	m_splashTexture = new sf::Texture();
	m_splashTexture->loadFromFile("Assets/Images/Obstacle_Splash.png");

}

void TerrainManager::Update(Player* player){
	srand((unsigned)time(0)); 
    int random_integer = (rand()%4)+1; 
	int random_integer2 = (rand()%3)+1;

	m_deltaTime = m_clock->restart().asSeconds();
	m_timer += m_deltaTime;	
	m_spawnTimer += m_deltaTime;

	if(m_spawnTimer < 30.f){
		m_spawnTime = 3.5f;
	}else if(m_spawnTimer < 60.f){
		m_spawnTime = 3.0f;
	} else if(m_spawnTimer < 90.f){
		m_spawnTime = 2.5f;
	} else if(m_spawnTimer < 150.f){
		m_spawnTime = 2.0f;
	} else if(m_spawnTimer < 180.f){
		m_spawnTime = 2.0f;
	} else{
		m_spawnTime = 2.0f;
	}

	if(m_timer > m_spawnTime){
		terrainCount++;
		Barrel* barrelObj = new Barrel;
		if(random_integer2 == 1){
			barrelObj->Initialize(m_renderWindow, random_integer, m_brlTexture, m_splashTexture, m_bloodTexture);
		}else if(random_integer2 == 2){
			barrelObj->Initialize(m_renderWindow, random_integer, m_stoneTexture, m_splashTexture, m_bloodTexture);
		}else if(random_integer2 == 3){
			barrelObj->Initialize(m_renderWindow, random_integer, m_mineTexture, m_splashTexture, m_bloodTexture);
		}
		m_arrayOfTerrain.push_back(barrelObj);
		m_timer = 0.f;
	}
	
	if(m_arrayOfTerrain.size() == 0){
		terrainCount = 0;
	}

	for (int i = 0; i <m_arrayOfTerrain.size(); i++) {
		if(m_arrayOfTerrain[i]->m_isDead == true) {
			m_arrayOfTerrain[i]->Cleanup();
			delete m_arrayOfTerrain[i];
			m_arrayOfTerrain.erase(m_arrayOfTerrain.begin() + i);
		}
	}
	
	for(int i = 0; i < m_arrayOfTerrain.size(); i++) {	
		m_arrayOfTerrain[i]->Update(player);	
	}


}

void TerrainManager::Reset(){	
	m_spawnTimer = 0.f;
	m_spawnTime = 3.5f;
	for(int i = 0; i < m_arrayOfTerrain.size(); i++) {	
		//m_arrayOfTerrain[i]->Reset();	
		m_arrayOfTerrain[i]->m_isDead = true;
	}
	for (int i = 0; i <m_arrayOfTerrain.size(); i++) {
		if(m_arrayOfTerrain[i]->m_isDead == true) {
			m_arrayOfTerrain[i]->Cleanup();
			delete m_arrayOfTerrain[i];
			m_arrayOfTerrain.erase(m_arrayOfTerrain.begin() + i);
		}
	}
}

void TerrainManager::Cleanup(){
	for (int i = 0; i <m_arrayOfTerrain.size(); i++) {
		m_arrayOfTerrain[i]->Cleanup();
		delete m_arrayOfTerrain[i];
		m_arrayOfTerrain.erase(m_arrayOfTerrain.begin() + i);
	}

	delete m_stoneTexture;
	delete m_brlTexture;
	delete m_splashTexture;
	delete m_clock;
	delete m_bloodTexture;
}